package com.servlet;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.AnnouncesDao;
import com.model.Announces;

@WebServlet("/MakeBodySer")
public class MakeBodySer extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public MakeBodySer() {
		super();

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id"); 
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=\"UTF-8\"");
        
		int aid = Integer.valueOf(id);
		System.out.println(aid);
		Writer out = response.getWriter();
		AnnouncesDao ad = new AnnouncesDao();
		Announces a = new Announces();
		a.setAnnouncesID(aid);
		List<Announces> aList = ad.byIDonSomething(a);
		String str = "[";
		System.out.println(aList.size());
		for (Announces an : aList) {
			// System.out.println(an.getAnnouncesID()+"\t"+an.getAnnouTitle()+"\t"+an.getAnnouBody()+"\t"+an.getFile());
			str += "{\"id\":" + an.getAnnouncesID() + ",\"title\":\"" + an.getAnnouTitle() + "\",\"body\":\""
					+ an.getAnnouBody() + "\",\"file\":\"" + an.getFile() + "\"},";

		}
		String str1 = str.substring(0, str.length() - 1);
		str1 += "]";
		out.write(str1);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
